<?php

class Login_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->library('session');
    }

    public function login($email, $passwd)
    {
        $passwdHash = md5($passwd);

        $query = $this->db
            ->where(['email' => $email, 'passwd' => $passwdHash])
            ->select('users.id, users.email, users.group, users.active, groups.access_admin, groups.access_users, groups.access_groups')
            ->join('groups', 'users.group = groups.id', 'left')
            ->from('users')
            ->get();

        $result = $query->row();

        if (!isset($result->id))
        {
            $this->session->set_userdata('loginErrorCode','01');
            return false;
        }

        if ($result->access_admin != "Y" || $result->active != 'Y')
        {
            $this->session->set_userdata('loginErrorCode','02');
            return false;
        }

        return $result;
    }
}